package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.GoldInfo;
import com.haotunet.app.goldtreasure.model.StoreInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;

public class PickGoldActivity extends BaseActivity implements OnClickListener {
	private TitleView mTitleView;
	private Button btnNext;
	private TextView mPickNum;
	private TextView mExtractableGold;
	private RelativeLayout plus_20_btn, plus_50_btn, plus_100_btn, plus_200_btn, plus_500_btn, plus_1000_btn;
	private RelativeLayout minus_20_btn, minus_50_btn, minus_100_btn, minus_200_btn, minus_500_btn, minus_1000_btn;
	private TextView tvGrams20, tvGrams50, tvGrams100, tvGrams200, tvGrams500, tvGrams1000;
	private int gram20 = 0;
	private int gram50 = 0;
	private int gram100 = 0;
	private int gram200 = 0;
	private int gram500 = 0;
	private int gram1000 = 0;
	private StoreInfo info;
	private String shopId;
	private String type;
	private double mPickGold;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pick_gold);
		initView();
		initData();
	}

	private void initData() {
		ActionUtil actionUtil = new ActionUtil(PickGoldActivity.this);
		actionUtil.getMyTotalGold();
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (isFinishing()){
					return;
				}
				GoldInfo info = (GoldInfo) model;

				if (info == null) {
					UIHelper.showToast(PickGoldActivity.this, getString(R.string.rank_fail));
					return;
				}
				if (info.getErrorMsg() != null) {
					if (info.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
						ActivityUtils.forceQuit(PickGoldActivity.this,getString(R.string.crowding_in_tips_txt));
					} else {
						UIHelper.showToast(PickGoldActivity.this, info.getErrorMsg());
					}
				} else {
					mExtractableGold.setText("可提克数:" + info.getAvailableGold() + "克");
					mPickGold = Double.parseDouble(info.getAvailableGold());
					if (mPickGold < 20) {
						UIHelper.showBuyRemindDialog(PickGoldActivity.this, "可用流动金小于最小提金克数（20克），请先购买。",1);
					}
				}
			}
		});
	}

	private void initView() {
		Intent intent = getIntent();
		type = intent.getStringExtra("type");
		if (type.equals("1")) {
			info = (StoreInfo) getIntent().getSerializableExtra("storeDetailInfo");
			shopId = getIntent().getStringExtra("shopId");
		}
		mTitleView = (TitleView) findViewById(R.id.title_view);
		mTitleView.getTitletextView().setVisibility(View.VISIBLE);
		mTitleView.getTitletextView().setText(getString(R.string.proposed_gold));
		mTitleView.getBackView().setVisibility(View.VISIBLE);
		mTitleView.getBackView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mPickNum = (TextView) findViewById(R.id.pick_gold_num);
		mExtractableGold = (TextView) findViewById(R.id.extractable_gold);
		minus_20_btn = (RelativeLayout) findViewById(R.id.minus_20);
		tvGrams20 = (TextView) findViewById(R.id.txt_grams_20);
		plus_20_btn = (RelativeLayout) findViewById(R.id.plus_20);
		minus_50_btn = (RelativeLayout) findViewById(R.id.minus_50);
		tvGrams50 = (TextView) findViewById(R.id.txt_grams_50);
		plus_50_btn = (RelativeLayout) findViewById(R.id.plus_50);
		minus_100_btn = (RelativeLayout) findViewById(R.id.minus_100);
		tvGrams100 = (TextView) findViewById(R.id.txt_grams_100);
		plus_100_btn = (RelativeLayout) findViewById(R.id.plus_100);
		minus_200_btn = (RelativeLayout) findViewById(R.id.minus_200);
		tvGrams200 = (TextView) findViewById(R.id.txt_grams_200);
		plus_200_btn = (RelativeLayout) findViewById(R.id.plus_200);
		minus_500_btn = (RelativeLayout) findViewById(R.id.minus_500);
		tvGrams500 = (TextView) findViewById(R.id.txt_grams_500);
		plus_500_btn = (RelativeLayout) findViewById(R.id.plus_500);
		minus_1000_btn = (RelativeLayout) findViewById(R.id.minus_1000);
		tvGrams1000 = (TextView) findViewById(R.id.txt_grams_1000);
		plus_1000_btn = (RelativeLayout) findViewById(R.id.plus_1000);
		btnNext = (Button) findViewById(R.id.btn_next);
		setListener();

	}

	private void setListener() {
		minus_20_btn.setOnClickListener(this);
		plus_20_btn.setOnClickListener(this);
		minus_50_btn.setOnClickListener(this);
		plus_50_btn.setOnClickListener(this);
		minus_100_btn.setOnClickListener(this);
		plus_100_btn.setOnClickListener(this);
		minus_200_btn.setOnClickListener(this);
		plus_200_btn.setOnClickListener(this);
		minus_500_btn.setOnClickListener(this);
		plus_500_btn.setOnClickListener(this);
		minus_1000_btn.setOnClickListener(this);
		plus_1000_btn.setOnClickListener(this);
		btnNext.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.minus_20:
			if (gram20 > 0) {
				gram20--;
			}
			tvGrams20.setText(gram20 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.plus_20:
			gram20++;
			tvGrams20.setText(gram20 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.minus_50:
			if (gram50 > 0) {
				gram50--;
			}
			tvGrams50.setText(gram50 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.plus_50:
			gram50++;
			tvGrams50.setText(gram50 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.minus_100:
			if (gram100 > 0) {
				gram100--;
			}
			tvGrams100.setText(gram100 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.plus_100:
			gram100++;
			tvGrams100.setText(gram100 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.minus_200:
			if (gram200 > 0) {
				gram200--;
			}
			tvGrams200.setText(gram200 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.plus_200:
			gram200++;
			tvGrams200.setText(gram200 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.minus_500:
			if (gram500 > 0) {
				gram500--;
			}
			tvGrams500.setText(gram500 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.plus_500:
			gram500++;
			tvGrams500.setText(gram500 + "");
			mPickNum.setText((gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500) + "克");
			break;
		case R.id.minus_1000:
			if (gram1000 > 0) {
				gram1000--;
			}
			tvGrams1000.setText(gram1000 + "");
			mPickNum.setText(
					(gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500 + gram1000 * 1000)
							+ "克");
			break;
		case R.id.plus_1000:
			gram1000++;
			tvGrams1000.setText(gram1000 + "");
			mPickNum.setText(
					(gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500 + gram1000 * 1000)
							+ "克");
			break;
		case R.id.btn_next:
			if (gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500 + gram1000 * 1000 == 0) {
				UIHelper.showToast(PickGoldActivity.this, "提金克数不能为0!");
				return;
			}
			if (gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500
					+ gram1000 * 1000 > mPickGold) {
				UIHelper.showBuyRemindDialog(PickGoldActivity.this, "您的可提金克数小于提金数,请先购买",1);
				return;
			}
			if (type.equals("0")) {
				Intent nextIntent = new Intent(PickGoldActivity.this, PickGoldNextActivity.class);
				nextIntent.putExtra("grams20", gram20);
				nextIntent.putExtra("grams50", gram50);
				nextIntent.putExtra("grams100", gram100);
				nextIntent.putExtra("grams200", gram200);
				nextIntent.putExtra("grams500", gram500);
				nextIntent.putExtra("grams1000", gram1000);
				startActivity(nextIntent);
			} else {
				ActivityUtils.gotoStorePick(PickGoldActivity.this, info, shopId, gram20, gram50, gram100, gram200,
						gram500, gram1000);
			}

			break;

		default:
			break;
		}
	}
}
